/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CKhuyen_mai;
import DTO.CFields;
import DTO.CHistory;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author quangphuc
 */
public class CKhuyenMaiDAO extends IData<CKhuyen_mai>{

    @Override
    public CKhuyen_mai add(CKhuyen_mai item) {
         try {
           ps.setString(1, item.getLoaiKm());
           ps.setTimestamp(2, item.getTuNgay());
           ps.setTimestamp(3, item.getDenNgay());          
           ps.setBoolean(4, item.getApDung());
           ps.setString(5, item.getMatHangMua());
           ps.setString(6, item.getMatHangTang());
           ps.setDouble(7, item.getGiamGia());
           ps.setBoolean(8, item.getLoaiKH_AD());
           ps.setString(9, item.getMota());
           ps.setString(10, item.getNguoiTao());
           ps.setTimestamp(11, item.getNgayTao());
           ps.setLong(12, item.getKmId());
           ps.setInt(13, 0);//so luong mua
           ps.setInt(14, 0);//so luong tang
           ps.addBatch();
       } catch (SQLException ex) {
           Logger.getLogger(CKhuyenMaiDAO.class.getName()).log(Level.SEVERE, null, ex);
           throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
       }
       // 
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CKhuyen_mai> list, SubConfig config) {
        String mysql ="insert into  " + CFields.tb_khuyen_mai + "(" 
                + CFields.loai_km + "," + CFields.tu_ngay + ","
                + CFields.den_ngay + "," + CFields.ap_dung + ","
                + CFields.mat_hang_mua + "," + CFields.mat_hang_tang + ","
                + CFields.giam_gia + "," + CFields.loai_kh_ap_dung + "," 
                + CFields.mo_ta + "," + CFields.nguoi_tao + ","                
                + CFields.ngay_tao + "," + CFields.km_id + ","
                + CFields.sl_mua + "," + CFields.sl_tang
                +") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        int rs = 0;
         try {
            ps = config.connec.prepareStatement(mysql);
            for(CKhuyen_mai item :list){
                this.add(item);
            }
            int []n = ps.executeBatch();
            rs = n.length;
        } catch (SQLException ex) {
             System.out.println("sss loi : " + ex.getMessage());
            Logger.getLogger(CHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
        }
        return rs;
    }

    @Override
    public CKhuyen_mai Find(CKhuyen_mai item, List<CKhuyen_mai> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(CKhuyen_mai item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object insertOnSubmit(CKhuyen_mai item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object updateOnSubmint(CKhuyen_mai item, SubConfig config) {
        Object result = 0;
        String mysql ="update " + CFields.tb_khuyen_mai + " set "
                + CFields.loai_km + "=?," + CFields.mat_hang_mua + "=?,"
                + CFields.mat_hang_tang + "=?,"+ CFields.mo_ta+"=?,"
                + CFields.ap_dung + "=?," + CFields.den_ngay + "=?,"
                + CFields.tu_ngay + "=?," + CFields.giam_gia + "=?,"
                + CFields.loai_kh_ap_dung + "=?,"+ CFields.km_id +"=?,"
                + CFields.nguoi_sua + "=?," + CFields.ngay_sua + "?,"
                + " where " + CFields.id + " =? ";        
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setString(1, item.getLoaiKm());
            cs.setString(2, item.getMatHangMua());
            cs.setString(3, item.getMatHangTang());
            cs.setString(4, item.getMota());            
            cs.setBoolean(5, item.getApDung());
            cs.setTimestamp(6,item.getDenNgay());
            cs.setTimestamp(7, item.getTuNgay());
            cs.setDouble(8, item.getGiamGia());
            cs.setBoolean(9, item.getLoaiKH_AD());
            cs.setLong(10, item.getKmId());
            cs.setString(11, item.getNguoiSua());
            cs.setTimestamp(12, item.getNgaySua());
            cs.setLong(13, item.getId());
            result = cs.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CKhuyenMaiDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
        return result;
    }

    @Override
    public Object deleteOnSubmint(CKhuyen_mai item, SubConfig config) {
        Object result = 0;
        String mysql ="";
        
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setString(1, item.getLoaiKm());
            cs.setString(2, item.getMatHangMua());
            cs.setString(3, item.getMatHangTang());
            cs.setString(4, item.getMota());
            cs.setString(5, item.getNguoiTao());
            cs.setBoolean(6, item.getApDung());
            cs.setTimestamp(7,item.getDenNgay());
            cs.setTimestamp(8, item.getTuNgay());
            cs.setDouble(9, item.getGiamGia());
            cs.setBoolean(10, item.getLoaiKH_AD());
            cs.setLong(11, item.getKmId());
            cs.setString(12, item.getNguoiTao());
            cs.setTimestamp(13, item.getNgayTao());
            result = cs.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CKhuyenMaiDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
        return result;
    }

    @Override
    protected CKhuyen_mai getItem(ResultSet rs) {
        CKhuyen_mai item = new CKhuyen_mai();
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            item.setApDung(rs.getBoolean(CFields.ap_dung));
            item.setDenNgay(rs.getTimestamp(CFields.den_ngay));
            item.setGiamGia(rs.getDouble(CFields.giam_gia));
            item.setId(rs.getLong(CFields.id));
            item.setLoaiKH_AD(rs.getBoolean(CFields.loai_kh_ap_dung));
            item.setLoaiKm(rs.getString(CFields.loai_km));
            item.setMatHangMua(rs.getString(CFields.mat_hang_mua));
            item.setMatHangTang(rs.getString(CFields.mat_hang_tang));
            item.setMoTa(rs.getString(CFields.mo_ta));
            item.setTuNgay(rs.getTimestamp(CFields.tu_ngay));
            item.setKmId(rs.getLong(CFields.km_id));
        } catch (SQLException ex) {
            Logger.getLogger(CKhuyenMaiDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
        }
        
        return item;
    }

    @Override
    public List<CKhuyen_mai> getAll(SubConfig config) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        this.sWhere = "," + CFields.tb_loai_khuyen_mai + " lkm where lkm."+ CFields.id 
                + "= km." + CFields.km_id + " and lkm." + CFields.active + "=true";
        String orderby =" order by lkm." + CFields.id + " asc";
        String mysql = "select km.* from " + CFields.tb_khuyen_mai + " km " + this.sWhere + orderby;
        return this.getAll(mysql, config);
    }        

    @Override
    protected void add(CKhuyen_mai item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  
}
